package cn.mandy.sysadmin.controller;

import cn.mandy.sysadmin.common.api.CommonResult;
import cn.mandy.sysadmin.common.api.ResultCode;
import cn.mandy.sysadmin.component.JwtAuthenticationTokenFilter;
import cn.mandy.sysadmin.common.api.MandyRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class MandyControllerAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(JwtAuthenticationTokenFilter.class);


    //自定义异常
    @ExceptionHandler({MandyRuntimeException.class})
    public CommonResult requestTypeMismatch(MandyRuntimeException ex) {
        return CommonResult.failed(ex);
    }

    //400错误-缺失参数
    @ExceptionHandler({MissingServletRequestParameterException.class})
    public CommonResult requestMissingServletRequest(MissingServletRequestParameterException ex) {
        return CommonResult.failed(ResultCode.VALIDATE_FAILED);
    }

    //其他错误
    @ExceptionHandler({Exception.class})
    public CommonResult exception(Exception ex) {
        return CommonResult.failed(ResultCode.FAILED);
    }


}

